package su.product.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.product.dao.ICateDAO;
import su.product.entity.Cate;


@Repository
public class CateDAO extends SuBaseDAO implements ICateDAO {
	
	
	
	public List<Cate> getByPid(int pid) throws SQLException {
		String sql = "select * from su_product_cate where status=1 and pid=? order by id";
		
		return super.query(sql,new Object[]{pid}, new RowMapper<Cate>() {

			public Cate mapRow(ResultSet rs, int arg1) throws SQLException {
				Cate entity = new Cate();
				entity.setId(rs.getInt("id"));
				entity.setCatename(rs.getString("catename"));
				entity.setPid(rs.getInt("pid"));
				entity.setStatus(rs.getInt("status"));
				entity.setLeaf(rs.getInt("leaf"));
				entity.setProductnum(rs.getInt("productnum"));
				return entity;
			}
			
		});
	}

	
	
	public Cate getById(int id) throws SQLException {
		String sql = "select * from su_product_cate where status=1 and id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<Cate>() {
			public Cate mapRow(ResultSet rs, int value) throws SQLException {
				Cate entity = new Cate();
				entity.setId(rs.getInt("id"));
				entity.setCatename(rs.getString("catename"));
				entity.setPid(rs.getInt("pid"));
				entity.setStatus(rs.getInt("status"));
				entity.setLeaf(rs.getInt("leaf"));
				entity.setProductnum(rs.getInt("productnum"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "update su_product_cate set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
	
	
	public int insert(Cate o) throws SQLException {
		String sql = "insert into su_product_cate(catename ,pid ,status ,leaf ,productnum ) values(? ,? ,? ,? ,? )";
		KeyHolder kh = super.updateBackKey(sql, new Object[]{o.getCatename() ,o.getPid() ,o.getStatus() ,o.getLeaf() ,o.getProductnum() });
		if(kh==null)return 0;
		return kh.getKey().intValue();
	}
	
	
	
	public void update(Cate o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_product_cate set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getCatename());
		updateSql.append("catename=?").append(",");
		args.add(o.getPid());
		updateSql.append("pid=?").append(",");
		args.add(o.getStatus());
		updateSql.append("status=?").append(",");
		args.add(o.getLeaf());
		updateSql.append("leaf=?").append(",");
		args.add(o.getProductnum());
		updateSql.append("productnum=?").append(",");
			
		
		args.add(o.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}
	
	public void updateCateName(int id,String name) throws SQLException{
		String sql = "update su_product_cate set catename=? where id=?";
		super.update(sql, new Object[]{name,id});
	}
	
	public void updateCateLeaf(int cateId,int leaf)throws SQLException{
		String sql = "update su_product_cate set leaf=? where id=?";
		super.update(sql, new Object[]{leaf,cateId});
	}
	
	public void updateProductNum(int cateId,boolean isAdd) throws Exception{
		String sql = "update su_product_cate set productnum=";
		if(isAdd)sql += "productnum+1";
		else sql += "productnum-1";
		sql += " where id="+cateId;
		super.update(sql);
	}

}

